home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / UUPC11QS.ARJ / NBSTIME.C < prev    next >
C/C++ Source or Header  |  1991-10-18  |  9KB  |  244 lines

  1. /*--------------------------------------------------------------------*/
  2. /*    n b s t i m e . c                                               */
  3. /*                                                                    */
  4. /*    Set local system clock from National Bureau of Standards        */
  5. /*    Standard Time service                                           */
  6. /*                                                                    */
  7. /*    Copyright (c) 1991, Andrew H. Derbyshire                        */
  8. /*    See README.PRN for distribution restrictions and additional     */
  9. /*    copyrights                                                      */
  10. /*--------------------------------------------------------------------*/
  11.  
  12. /*--------------------------------------------------------------------*/
  13. /*                        System include files                        */
  14. /*--------------------------------------------------------------------*/
  15.  
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <time.h>
  19. #include <string.h>
  20.  
  21.  
  22. #ifdef FAMILYAPI
  23. #define INCL_BASE
  24. #include <os2.h>
  25. #else
  26. #ifndef __TURBOC__
  27. #include <dos.h>
  28. #endif
  29. #endif
  30.  
  31.  
  32. /*--------------------------------------------------------------------*/
  33. /*                    UUPC/extended include files                     */
  34. /*--------------------------------------------------------------------*/
  35.  
  36. #include "lib.h"
  37. #include "arpadate.h"
  38. #include "dcp.h"
  39. #include "dcpsys.h"
  40. #include "hostable.h"
  41. #include "nbstime.h"
  42. #include "script.h"
  43. #include "security.h"
  44. #include "ulib.h"
  45.  
  46. #ifndef __TURBOC__
  47.       currentfile();
  48. #endif
  49. /*--------------------------------------------------------------------*/
  50. /*    n b s t i m e                                                   */
  51. /*                                                                    */
  52. /*    Set system clock from using time from NBS of the format:        */
  53. /*                                                                    */
  54. /*                  MJD  YR MO DA  H  M  S ST S UT1 msADV         OTM */
  55. /*  nbs format-->  47511 88-12-16 06:03:44 00 0 -.1 045.0 UTC(NIST) * */
  56. /*  @ 1200 baud    47511 88-12-16 06:03:45 00 0 -.1 045.0 UTC(NIST) * */
  57. /*--------------------------------------------------------------------*/
  58.  
  59. boolean nbstime( void )
  60. {
  61.    char buf[BUFSIZ];
  62.    time_t today;
  63.    struct tm  tx;
  64.    int cycles = 15;
  65.    int dst= 0;
  66.    time_t delta;
  67.    char sync = '?';
  68. #ifdef FAMILYAPI
  69.    DATETIME DateTime;
  70.    struct tm *tp;
  71.    USHORT rc;
  72. #else
  73. #ifndef __TURBOC__
  74.    unsigned short rc;
  75.    struct tm *tp;
  76.    struct dosdate_t ddate;
  77.    struct dostime_t dtime;
  78. #endif
  79. #endif
  80.  
  81.    memset( &tx , '\0', sizeof tx);        /* Clear pointers         */
  82.    if (!expectstr("MJD", 5 ))    /* Margaret Jane Derbyshire? :-)    */
  83.    {
  84.       printmsg(0,"nbstime: Did not find MJD literal in data from remote");
  85.       return FALSE;
  86.    }
  87.  
  88.    rmsg(buf, 2, 2);     /* Read one line to get us setup for input   */
  89.  
  90. /*--------------------------------------------------------------------*/
  91. /*                  Begin main loop to get the time                   */
  92. /*--------------------------------------------------------------------*/
  93.  
  94.    while ((rmsg(buf, 2, 2) != TIMEOUT) && cycles--)
  95.    {
  96.       sync = buf[ strlen( buf ) - 1 ];
  97.  
  98.       if (sync == '#')
  99.          break;
  100.       else if (sync != '*')
  101.          *buf = '\0';
  102.  
  103.    } /* while */
  104.  
  105.    if ( (cycles && (sync == '*')) || (*buf == '\0'))
  106.    {
  107.       printmsg(0,"nbstime: Did not get good buffer: \"%s\"", buf );
  108.       return FALSE;
  109.    }
  110.  
  111. /*--------------------------------------------------------------------*/
  112. /*                   Determine the time we received                   */
  113. /*--------------------------------------------------------------------*/
  114.  
  115.    sscanf(buf,"%*s %d-%d-%d %d:%d:%d %d ",
  116.          &tx.tm_year, &tx.tm_mon, &tx.tm_mday ,
  117.          &tx.tm_hour, &tx.tm_min, &tx.tm_sec, &dst);
  118.    tx.tm_mon--;               /* Tm record counts months from zero   */
  119.  
  120.    today = mktime(&tx);       /* Current UTC (GMT) time in seconds   */
  121.  
  122.    if ( debuglevel > 2 )
  123.    {
  124.       printmsg(3,"%2d/%2d/%2d %2d:%2d:%2d %2d %c translates to %ld or %s",
  125.          tx.tm_year, tx.tm_mon + 1 , tx.tm_mday ,
  126.          tx.tm_hour, tx.tm_min, tx.tm_sec, dst, sync ,
  127.          today, ctime( &today ));
  128.    }
  129.  
  130. /*--------------------------------------------------------------------*/
  131. /*    Perform a sanity check; the time must be 20 years past 1970     */
  132. /*--------------------------------------------------------------------*/
  133.  
  134.    if ( today < 630720000L )
  135.    {
  136.       printmsg(0,"nbstime: Time warp error (%s), clock not set",
  137.             ctime( &today ));
  138.       return FALSE;
  139.    }
  140.  
  141. /*--------------------------------------------------------------------*/
  142. /*     Borland C++ doesn't set the time properly; do a conversion     */
  143. /*--------------------------------------------------------------------*/
  144.  
  145.    today -= timezone;
  146.  
  147. /*--------------------------------------------------------------------*/
  148. /*                        Set the system clock                        */
  149. /*--------------------------------------------------------------------*/
  150.  
  151. #ifdef FAMILYAPI
  152.    tp = localtime(&today);    /* Get local time as a record          */
  153.    rc = DosGetDateTime( &DateTime );
  154.    if ( rc != 0 )
  155.    {
  156.       printmsg(0,"Return code from DosGetDateTime %d", rc);
  157.       panic();
  158.    }
  159.    printmsg(3,"Date time: %2d/%2d/%2d %2d:%2d:%2d tz %d, weekday %d",
  160.       (int) DateTime.year, (int) DateTime.month, (int) DateTime.day ,
  161.       (int) DateTime.hours, (int) DateTime.minutes,(int) DateTime.seconds ,
  162.       (int) DateTime.timezone, (int) DateTime.weekday );
  163.  
  164.    DateTime.year    = (USHORT) tp->tm_year + 1900;
  165.    DateTime.month   = (UCHAR) (tp->tm_mon + 1);
  166.    DateTime.day     = (UCHAR) tp->tm_mday;
  167.    DateTime.hours   = (UCHAR) tp->tm_hour;
  168.    DateTime.minutes = (UCHAR) tp->tm_min;
  169.    DateTime.seconds = (UCHAR) tp->tm_sec;
  170.    printmsg(3,"Date time: %2d/%2d/%2d %2d:%2d:%2d tz %d, weekday %d",
  171.       (int) DateTime.year, (int) DateTime.month, (int) DateTime.day ,
  172.       (int) DateTime.hours, (int) DateTime.minutes,(int) DateTime.seconds ,
  173.       (int) DateTime.timezone, (int) DateTime.weekday );
  174.    rc = DosSetDateTime( &DateTime );
  175.    if ( rc != 0 )
  176.    {
  177.       printmsg(0,"Return code from DosGetDateTime %d", rc);
  178.       panic();
  179.    }
  180. #else
  181. #ifdef __TURBOC__
  182. /*--------------------------------------------------------------------*/
  183. /*    If this timezone uses daylight savings and we are in the        */
  184. /*    period to spring forward, do so.                                */
  185. /*--------------------------------------------------------------------*/
  186.  
  187.    if (daylight && ( dst > 1 ) && ( dst < 52 ))
  188.       today += 3600;          /* This is valid for the USA only      */
  189.    stime( &today );
  190. #else
  191.    tp = localtime(&today);    /* Get local time as a record          */
  192.  
  193.    ddate.day     = (unsigned char) tp->tm_mday;
  194.    ddate.month   = (unsigned char) (tp->tm_mon + 1);
  195.    ddate.year    = (unsigned int)  (tp->tm_year + 1900);
  196.    ddate.dayofweek = (unsigned char) tp->tm_wday;       /* 0-6, 0=Sunday */
  197.  
  198.    dtime.hour    = (unsigned char) tp->tm_hour;
  199.    dtime.minute  = (unsigned char) tp->tm_min;
  200.    dtime.second  = (unsigned char) tp->tm_sec;
  201.    dtime.hsecond = (unsigned char) 0;
  202.  
  203.    printmsg(3,"Date time: %2d/%2d/%2d %2d:%2d:%2d tz %d, weekday %d",
  204.       (int) ddate.year, (int) ddate.month, (int) ddate.day ,
  205.       (int) dtime.hour, (int) dtime.minute,(int) dtime.second ,
  206.       (int) timezone, (int) ddate.dayofweek );
  207.  
  208.    if ( (rc = _dos_settime( &dtime )) != 0 )
  209.    {
  210.       printmsg(0,"Return code from _dos_settime %d", rc);
  211.       panic();
  212.    }
  213.  
  214.    if ( (rc = _dos_setdate( &ddate )) != 0 )
  215.    {
  216.       printmsg(0,"Return code from _dos_setdate %d", rc);
  217.       panic();
  218.    }
  219.  
  220. #endif
  221. #endif
  222.  
  223. /*--------------------------------------------------------------------*/
  224. /*             Print debugging information, if requested              */
  225. /*--------------------------------------------------------------------*/
  226.  
  227.    delta = today - time( NULL );
  228.    printmsg(2,"nbstime: \"%s\"", buf);
  229.    printmsg(2,"nbstime: Time delta is %ld seconds, zone offset %ld, \
  230. daylight savings %d",
  231.                   delta, timezone, dst );
  232.  
  233.    if ( sync == '*' )
  234.       printmsg(2,"Warning: Was unable to synchonize with NBS master");
  235.  
  236. /*--------------------------------------------------------------------*/
  237. /*                Announce new time, return to caller                 */
  238. /*--------------------------------------------------------------------*/
  239.  
  240.    printmsg(0,"nbstime: New system time is %s", arpadate());
  241.    return TRUE;
  242.  
  243. } /* nbstime */
  244.